﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Warlock.GameStuff
{
    /// <summary>
    /// Objekt das jeder Client bekommt um die anderen Spieler zu zeichnen
    /// </summary>
    class PlayerManager
    {
        //Alle Spieler
        public static SortedList<int, Player> playerlist = new SortedList<int, Player>();
        static Player[] playerManager = new Player[20];
        //Array-Index des eigene Spielers
        public static int currentPlayer = 0;
        static int[] freeslots = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

        public static int AddPlayer(Player newPlayer)
        {
            Console.WriteLine("neuerPlayer: " + newPlayer.Name);
            int id = 0;


            int counter = 0;
            while (counter <= 20)
            {
                if (freeslots[counter] != 0)
                {
                    id = freeslots[counter];
                    freeslots[counter] = 0;
                    break;
                }
                counter++;
            }
            playerlist.Add(id, newPlayer);
            //playerManager[id - 1] = newPlayer;
            currentPlayer++;
            return id;
        }
        public static void RemovePlayer(int id)
        {
            playerlist.Remove(id);
            //playerManager[id] = new Player();
            freeslots[id - 1] = id;
            currentPlayer--;
        }
        public static void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            int counter = 1;
            while (counter <= currentPlayer)
            {
                playerlist[counter].Draw(spriteBatch, gameTime);
                //Console.WriteLine(playerlist[counter]);
                //playerManager[counter].Draw(spriteBatch, gameTime);
                counter++;
            }

        }

    }

    /// <summary>
    /// Die Klasse, die einen menschlichen Spieler darstellt.
    /// </summary>
    class Player : BaseCharacter
    {
        SpriteBatch PlayerBatch;
        public int Id;
        public int NetworkID = -1;
        public string texturePath;

        public Player()
        {
        }

        public Player(String texturePath, Vector2 startPos, String Name, int HP, GraphicsDevice graphicsDevice)
            : base()
        {
            PlayerBatch = new SpriteBatch(graphicsDevice);
            this.texturePath = texturePath;
            this.Texture = Texture2D.FromFile(graphicsDevice, texturePath);
            Position = startPos;
            Health = HP;
            name = Name;
            this.Scale = 1f;
            Id = PlayerManager.AddPlayer(this);
        }
        public Player(String Netstring, GraphicsDevice graphicsDevice)
            : base()
        {
            PlayerBatch = new SpriteBatch(graphicsDevice);
            String[] spliter = Netstring.Split(new char[] { ';' });
            if (spliter[0].Length > 2)
            {
                this.texturePath = spliter[0];
                this.Position = StringtoVector(spliter[1]);
                this.Name = spliter[2];
                this.Health = Convert.ToInt32(spliter[3]);
                this.Rotation = Convert.ToSingle(spliter[4]);
                this.Texture = Texture2D.FromFile(graphicsDevice, texturePath);
                this.Scale = 1f;
            }
            if (spliter[0].Length < 3)
            {
                NetworkID = Convert.ToInt32(spliter[0]);
                this.texturePath = spliter[1];
                this.Position = StringtoVector(spliter[2]);
                this.Name = spliter[3];
                this.Health = Convert.ToInt32(spliter[4]);
                this.Rotation = Convert.ToSingle(spliter[5]);
                this.Texture = Texture2D.FromFile(graphicsDevice, texturePath);
                this.Scale = 1f;
            }
            Id = PlayerManager.AddPlayer(this);
        }
        private Vector2 StringtoVector(String Vector)
        {
            String one = Vector.Replace("{", "");
            String Tow = one.Replace("}", "");
            String[] cords = new string[2];
            one = Tow.Replace("X", "");
            Tow = one.Replace("Y", "");
            one = Tow.Replace(":", "");
            cords = one.Split(new char[] { ' ' });
            Vector2 returner = new Vector2(Convert.ToSingle(cords[0]), Convert.ToSingle(cords[1]));
            return returner;
        }
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            PlayerBatch.Begin();
            PlayerBatch.Draw(Texture, Position, null, Color.White, Rotation, new Vector2(32, 32), this.Scale, SpriteEffects.None, 0);
            PlayerBatch.End();
        }
        public override void Update(GameTime gameTime)
        {
        }
        public void UpdateRotation(Vector2 endPos)
        {
            Vector2 startPos = this.Position;
            endPos.X = endPos.X - startPos.X;
            endPos.Y = endPos.Y - startPos.Y;
            Rotation = MathHelper.ToRadians((float)(Math.Atan2(endPos.Y, endPos.X) / (Math.PI / 180)));
        }
        public void UpdateMovement(Keys upKey, Keys DownKey, Keys leftKey, Keys rightKey)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(upKey))
            {
                Position += Vector2.Multiply(new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation)), 1);
            }
            if (keyboardState.IsKeyDown(DownKey))
            {
                Position -= Vector2.Multiply(new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation)), 1);
            }
            if (keyboardState.IsKeyDown(leftKey))
            {
                Position -= Vector2.Multiply(new Vector2((float)Math.Cos(Rotation + 1.5f), (float)Math.Sin(Rotation + 1.5f)), 1);
            }
            if (keyboardState.IsKeyDown(rightKey))
            {
                Position += Vector2.Multiply(new Vector2((float)Math.Cos(Rotation + 1.5f), (float)Math.Sin(Rotation + 1.5f)), 1);
            }
        }
        public override string ToString()
        {
            String returner;
            if (NetworkID != -1)
            {
                returner = NetworkID.ToString() + ";" + this.texturePath + ";" + this.Position.ToString() + ";" + this.Name + ";" + this.Health + ";" + this.Rotation;
            }
            else
            {
                returner = this.texturePath + ";" + this.Position.ToString() + ";" + this.Name + ";" + this.Health + ";" + this.Rotation;
            }
            return returner;
        }

    }
    class Playerinfo : BaseCharacter
    {
        public int Id;
        public int NetworkID = -1;
        public string texturePath;

        public Playerinfo()
        {
        }

        public Playerinfo(String Player, int NetID)
            : base()
        {
            String[] spliter = Player.Split(new char[] { ';' });
            this.texturePath = spliter[0];
            this.Position = StringtoVector(spliter[1]);
            this.Name = spliter[2];
            this.Health = Convert.ToInt32(spliter[3]);
            this.Rotation = Convert.ToSingle(spliter[4]);
            NetworkID = NetID;
        }
        public void PlayerinfoUpadate(String Player)
        {
            String[] spliter = Player.Split(new char[] { ';' });
            this.texturePath = spliter[1];
            this.Position = StringtoVector(spliter[2]);
            this.Name = spliter[3];
            this.Health = Convert.ToInt32(spliter[4]);
            this.Rotation = Convert.ToSingle(spliter[5]);
        }

        private Vector2 StringtoVector(String Vector)
        {
            String one = Vector.Replace("{", "");
            String Tow = one.Replace("}", "");
            String[] cords = new string[2];
            one = Tow.Replace("X", "");
            Tow = one.Replace("Y", "");
            one = Tow.Replace(":", "");
            cords = one.Split(new char[] { ' ' });
            Vector2 returner = new Vector2(Convert.ToSingle(cords[0]), Convert.ToSingle(cords[1]));
            return returner;
        }
        public override string ToString()
        {
            String returner;
            if (NetworkID != -1)
            {
                returner = NetworkID.ToString() + ";" + this.texturePath + ";" + this.Position.ToString() + ";" + this.Name + ";" + this.Health + ";" + this.Rotation;
            }
            else
            {
                returner = this.texturePath + ";" + this.Position.ToString() + ";" + this.Name + ";" + this.Health + ";" + this.Rotation;
            }
            return returner;
        }
    }
}